define([
	'jquery',
	'underscore',
	'backbone',
	'catjs/util',
	'dialog',
	'map',
	'function',
	],function($,_,Backbone,util,dialog,map,func){
	var module = {};

	//用户模型
	var User = Backbone.Model.extend({
		url : function(){
			var base = '/User/Default';
			//POST
			if( this.isNew() ){
				return base;
			}else{//GET,UPDATE,DELETE
				return base + '/' + this.id;
			}
		},

		login : function(view){
			var self = this;
			$.ajax({
				url : '/User/Login',
				type : 'POST',
				data : self.attributes,
				success : view.success,
				error : view.error,
			});
		},

	});

	//登录视图
	var LoginView = Backbone.View.extend({
		dialog : $('#dialog1'),
		shadow : $('#shadow1'),
		template : $('#tplLogin').text(),

		el : $('#dialog1 .dialog1-bd'),
		events : {
			'click #tabs a' : 'changeTab',
			'click #Login button[name=submit]' : 'login',
			'click #Register button[name=submit]' : 'register',
		},

		render : function(){
			var self = this;
			self.dialog.children('.dialog1-wrap').css('width','500px');
			self.$el.html(self.template);
			self.dialog.fadeIn();
			self.shadow.fadeIn();

			self.$el.find(".content div").hide();
			self.$el.find("#tabs li:first").attr("id","current");
			self.$el.find(".content div:first").fadeIn();
		},
		//注册或登录成功
		success : function(self){
			return function(user,response){
				self.dialog.fadeOut();
				self.shadow.fadeOut();
				//创建地图
				map.create(map.init_location);
				//创建全局控件
				func.init();
				//把用户信息存储到cookie
				if( typeof(user) === 'object'){//注册返回
					var u = new User({id:response});
					u.fetch({
						success : function(model,response){
							util.setUser(model);
						},
					});
				}else{
					util.setUser(user);
				}
			};
		},
		loginError : function(self){
			return function(){
				self.$el.find('#Login .err-msg-box').css('display','block');
			};
		},
		regError : function(self){
			return function(){
				self.$el.find('#Register .err-msg-box').css('display','block');
			};
		},
		changeTab : function(e){
			var self = this;
	        e.preventDefault();
	        if ($(e.toElement).closest("li").attr("id") == "current"){
	         return       
	        }
	        else{             
		        self.$el.find(".content div").hide();
		        self.$el.find("#tabs li").attr("id","");
		        $(e.toElement).parent().attr("id","current");
		        $('#' + $(e.toElement).attr('name')).fadeIn();
	        }
		},

		login : function(){
			var self = this;
			var email = self.$el.find('#Login input[name=email]').val();
			var password = self.$el.find('#Login input[name=password]').val();
			var user = new User({
				email : email,
				password : password,
   			});
			user.login({success:self.success(self),error:self.loginError(self)});
		},
		register : function(){
			var self = this;

			var email = self.$el.find('#Register input[name=email]').val();
			var password = self.$el.find('#Register input[name=password]').val();
			var nickname = self.$el.find('#Register input[name=nickname]').val();

			var user = new User({
				email : email,
				password : password,
				nickname : nickname,
			});
			user.save({},{success:self.success(self),error:self.regError(self)});
		},
	});

	//更新用户信息
	var updateUser = function(){
		var user = util.getUser();
		$.ajax({
			url : '/User/Default',
			type : 'GET',
			data : {
				id : user.userId,
			},
			success : function(json){
				util.setUser(json);
			},
		});
	};

	module.login = function(){
		if( util.readCookie('user') ){//已登陆
			//创建地图
			updateUser();
			map.create(map.init_location);
			//创建全局控件
			func.init();
		}else{//未登录
			var loginView = new LoginView;
			loginView.render();
		}
	};

	return module;
});